'use strict';
(function() {
    if ('scrollRestoration' in history) {
        history.scrollRestoration = 'manual';
    }

    var $storyMainBox = $('#J_StoryMainBox');
    var $storyContentList = $('#J_StoryContentList');
    var gCurrentDialogItemIndex = 0;
    var gIsClick = false;
    var gHasClickGuideHide = false;

    $(document)
        // use touchstart & touchmove & touchend to instead of click
        // because of QQWebView
        .on('touchstart', '#J_StoryContent', function() {
            gIsClick = true;
        })
        .on('touchmove', '#J_StoryContent', function() {
            gIsClick = false;
        })
        .on('touchend', '#J_StoryContent', function(evt) {
            if (!gIsClick) {
                return;
            }

            if ($(evt.target).hasClass('J_Download')) {
                return;
            }

            play();
        })
        // for non touch event browser
        .on('click', '#J_StoryContent', function() {
            if (gIsClick) {
                return;
            }
            play();
        })
        .on('click', '#J_GuideStoryClick', function() {
            if (gHasClickGuideHide) {
                return;
            }

            hideGuide();
            $('#J_StoryContent').trigger('click');
        })
        // for QQBrowser
        .on('touchstart', '#J_GuideStoryClick', function() {
            if (gHasClickGuideHide) {
                return;
            }

            hideGuide();
            $('#J_StoryContent').trigger('click');
        })
        .on('click', '.J_Download', function(evt) {
            evt.preventDefault();
            evt.stopPropagation();

            location.href = Crucio.appDownloadUrl || '/';
        });

    function scrollToBottom(force) {
        var $target = $('#J_StoryMainBox');

        if (!force && $target.hasClass(kVelocityAnimatingCls)) {
            return;
        }

        var container = $(window);
        var offset = Math.ceil(Math.max(0, $target.outerHeight() - container.height()));
        if (container.scrollTop() >= offset) {
            return;
        }

        $target.velocity('scroll', {
            offset: offset,
            duration: 400,
            easing: 'easeOutBack'
        });
    }

    function play() {
        if ($storyMainBox.hasClass('velocity-animating')) {
            return;
        }

        var $item = $storyContentList.children().eq(gCurrentDialogItemIndex);
        if ($item.length === 0) {
            var $box = $('#J_StoryMainBox');
            if (!$box.hasClass('the-end')) {
                $box.addClass('the-end');
                $('#J_StoryEndTips').fadeIn();
                $('#J_TopDownloadTips').fadeIn();
            } else {
                scrollToBottom(true);
            }
            return;
        }

        $item.css({
            display: 'block',
            opacity: '0'
        });

        var delta = $storyMainBox.outerHeight() - $(window).height();
        if (delta > 0) {
            $storyMainBox.velocity('scroll', {
                offset: Math.ceil(delta),
                duration: 400,
                complete: function() {
                    $item.css('opacity', 1);
                }
            });
            $('#J_TopDownloadTips').fadeIn();
        } else {
            $item.css('opacity', 1);
        }

        gCurrentDialogItemIndex += 1;

        if (gCurrentDialogItemIndex > 1) {
            hideGuide();
        }
    }

    function hideGuide() {
        if (gHasClickGuideHide) {
            return;
        }

        gHasClickGuideHide = true;
        $('#J_GuideStoryClick').velocity('fadeOut', { display: 'none' });
    }

    // show first dialog
    $('#J_StoryContent').trigger('click');
    // show story end popup if
    // if (/(from=timeline)|(from=singlemessage)|(from=groupmessage)/.test(location.search)) {
    //     if (!/MicroMessenger/.test(navigator.userAgent)) {
    //         toggleStoryEndPopup(true);
    //         location.href = $('#J_DownloadLink').attr('href');
    //     }
    // }

    // try to open app
    if (/android/i.test(navigator.userAgent)) {
        var sid = $('#J_StoryMainBox').data('sid');
        if (!sid) {
            return;
        }
        $('<iframe style="position:absolute;left:-9999em;top:-9999em;" src="crucio://story/' + sid + '">')
            .appendTo('body');
    }
})();

